# Contributor: nibon7 <nibon7@163.com>
# Maintainer: nibon7 <nibon7@163.com>
pkgname=v2ray
pkgver=5.12.1
pkgrel=1
pkgdesc="A platform for building proxies to bypass network restrictions"
url="https://v2fly.org"
arch="all"
license="MIT"
makedepends="go"
subpackages="$pkgname-openrc"
_geosite_ver=20231128180936
_geoip_ver=202311230040
source="$pkgname-core-$pkgver.tar.gz::https://github.com/v2fly/v2ray-core/archive/v$pkgver.tar.gz
	geosite-$_geosite_ver.dat::https://github.com/v2fly/domain-list-community/releases/download/$_geosite_ver/dlc.dat
	geoip-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip.dat
	geoip-only-cn-private-$_geoip_ver.dat::https://github.com/v2fly/geoip/releases/download/$_geoip_ver/geoip-only-cn-private.dat
	v2ray.initd
	"
options="net" # test FakeDNS

builddir="$srcdir/$pkgname-core-$pkgver"

export CGO_ENABLED=0
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	local ldflags="
		-X github.com/v2fly/v2ray-core/v5.codename=$pkgname
		-X github.com/v2fly/v2ray-core/v5.version=$pkgver
		-X github.com/v2fly/v2ray-core/v5.build=$SOURCE_DATE_EPOCH
		-buildid=
		"
	go build -trimpath -ldflags "$ldflags" -o v2ray ./main
}

check() {
	mkdir -p testing/temp
	cp "$srcdir"/geosite-$_geosite_ver.dat testing/temp/geosite.dat
	cp "$srcdir"/geoip-$_geoip_ver.dat testing/temp/geoip.dat

	# due to heavy CI workload, scenario tests always timeout
	local pkgs=$(go list ./... | grep -v \
		-e 'v5/testing/scenarios$' \
	)
	go test $pkgs
}

package() {
	install -Dm755 -d "$pkgdir"/etc/$pkgname
	install -m644 release/config/*.json -t "$pkgdir"/etc/$pkgname

	install -Dm755 -d "$pkgdir"/usr/share/$pkgname
	install -m644 "$srcdir"/geosite-$_geosite_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geosite.dat
	install -m644 "$srcdir"/geoip-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip.dat
	install -m644 "$srcdir"/geoip-only-cn-private-$_geoip_ver.dat \
		"$pkgdir"/usr/share/$pkgname/geoip-only-cn-private.dat

	install -Dm755 v2ray "$pkgdir"/usr/bin/v2ray

	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname
}

sha512sums="
4c152e93aaabe3eb61f594dc7840ecc958614fd3d43e8040aa5307509cd17a775d3e2b989321aafabee583aa87c6e4122f0563e43d62719f7e427f13c55c6858  v2ray-core-5.12.1.tar.gz
07ae25482a4b7b8a186438bb63f37aecd500a996eb297a99874234e303a6f18e38b07a24744192fc0caa42417ad86214a66cf9f94630f27524bad0f4f535bf7f  geosite-20231128180936.dat
fcce152129d552940b6e849f16bd89de53ecce14c7c16dfa1a7dbbb4db75ab47e5d3765ae9ff4597e93d845da7b5d992403a801caa14e3cad1a4acc32713d764  geoip-202311230040.dat
b644055a0f87fcdd06be2d104a77c169fd4f207d09f74a140759cb887c71bf18aabd97a41a3691b1757493e48dfdd55765d2645d895ce597f7079e0027f122af  geoip-only-cn-private-202311230040.dat
5b586b2a927da3a96c279bed3330bb9ee397b3e880e573ae7dc108d35864576cf72e5ceacb74b497829d514db10bb7f6db85c3e5383005288ab62cd61510ebaa  v2ray.initd
"
